<?php

namespace app\admin\model;

use think\Cache;
use think\Db;
use think\Model;

class QjNewQuestionLogModel extends Model
{


    public function delNewQuestionInfo($new_question_id)
    {
        Db::table('qj_new_question_log')->where(['id' => $new_question_id])->delete();
        $res['success'] = true;
        $res['desc'] = '删除成功';
        $res['errorcode'] = '';
        return $res;
    }
    public function getNewQuestionListByContent($content)
    {
        $where = [];
        $whereOr = [];
        if ($content) {
            $where['content'] = ['like', '%' . $content . '%'];
        }

        $ret = Db::table('qj_new_question_log')->where($where)->order('sort desc, id desc')->select();
        $parentIds = [];
        foreach ($ret as $val) {
            $parentIds[] = $val['id'];
        }
        $childList = Db::table('qj_new_question_log')->where('parent_id', 'in', $parentIds)->select();
        $type = 0;
        $notEndIds = [];
        if (empty($childList)) {
            $type = 1;
            foreach ($ret as &$item) {
                $item['type'] = $type;
            }
        } else {
            foreach ($childList as $v) {
                $notEndIds[] = $v['id'];
            }
            foreach ($ret as &$item) {
                if (!in_array($item['id'], $notEndIds)) {
                    $type = 1;
                }
                $item['type'] = $type;
            }
        }
        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        $res['data']['list'] = $ret;
        return $res;
    }

    public function getNewQuestionList($content, $pageSize, $page)
    {
        $where = [];
        if ($content) {
            $where['content'] = ['like', '%' . $content . '%'];
        }
        $offset = ($page - 1) * $pageSize;
        if ($offset <= 0) {
            $offset = 0;
        }
        $ret = Db::table('qj_new_question_log')->where($where)->limit($offset, $pageSize)->order('id desc')->select();
        $msgIds = [];
        foreach ($ret as $val) {
            $msgIds[] = $val['msg_id'];
        }

        $msgList = Db::table('qj_msg_log')->where('id', 'in', $msgIds)->order('id desc')->select();
        $msgListMap = [];
        foreach ($msgList  as $item) {
            $msgListMap[$item['id']] = $item;
        }
        foreach ($ret as &$v) {
            $v['askDesc'] = sprintf("%s 第 %d 次咨询", date('Y-m-d', strtotime($msgListMap[$v['msg_id']]['create_time'])), $v['msg_id']);
            $v['askTime'] = isset($msgListMap[$v['msg_id']]['create_time']) ? $msgListMap[$v['msg_id']]['create_time'] : date('Y-m-d', time());
        }
        $total = Db::table('qj_new_question_log')->where($where)->count();
        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        $total_page = 0;
        if ($total) {
            $total_page = ceil($total / $pageSize);
        }
        $res['data']['list'] = $ret;
        $res['data']['total'] = $total;
        $res['data']['total_page'] = $total_page;
        return $res;
    }

    public function getQuestionInfo($question_id)
    {
        $info =  Db::table('qj_new_question_log')->where(['id' => $question_id])->find();
        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        $res['data'] = $info;
        return $res;
    }
    public function getQuestionInfoNoPage($question_id)
    {
        $list =  Db::table('qj_new_question_log')->where(['parent_id' => $question_id, 'status' => 1])->select();
        $parentIds = [];
        foreach ($list as $val) {
            $parentIds[] = $val['id'];
        }
        $childList = Db::table('qj_new_question_log')->where('parent_id', 'in', $parentIds)->select();
        $type = 0;
        $notEndIds = [];
        if (empty($childList)) {
            $type = 1;
            foreach ($list as &$item) {
                $item['type'] = $type;
            }
        } else {
            foreach ($childList as $v) {
                $notEndIds[] = $v['id'];
            }
            foreach ($list as &$item) {
                if (!in_array($item['id'], $notEndIds)) {
                    $type = 1;
                }
                $item['type'] = $type;
            }
        }
        $res['success'] = true;
        $res['desc'] = '';
        $res['errorcode'] = '';
        $res['data']['list'] = $list;
        return $res;
    }
}
